/**
 * 不为空
 * @param val
 * @returns {boolean}
 */
export function notEmpty(val) {
  return !this.isEmpty(val);
}

/**
 * 为空
 * @param val
 * @returns {boolean}
 */
export function isEmpty(val) {
  if (
    val === null ||
    typeof val === 'undefined' ||
    (typeof val === 'string' && val === '' && val !== 'undefined')
  ) {
    return true;
  }
  return false;
}

/**
 * 数值型为空
 * @param val
 * @returns {boolean}
 */
export function isNumberEmpty(val) {
  if (
    val == 0 ||  val == -1 || val === null ||
    typeof val === 'undefined' ||
    (typeof val === 'string' && val === '' && val !== 'undefined')
  ) {
    return true;
  }
  return false;
}

/**
 * 强转int型
 * @param val
 * @param defaultValue
 * @returns {number}
 */
export function toInt(val, defaultValue) {
  if (this.isEmpty(val)) {
    return defaultValue === undefined ? -1 : defaultValue;
  }
  const num = parseInt(val, 0);
  return Number.isNaN(num) ? (defaultValue === undefined ? -1 : defaultValue) : num;
}

/**
 * Json强转为Form类型
 * @param obj
 * @returns {FormData}
 */
export function toFormData(obj) {
  const data = new FormData();
  Object.keys(obj).forEach(key => {
    data.append(key, Array.isArray(obj[key]) ? obj[key].join(',') : obj[key]);
  });
  return data;
}

/**
 * 根据逗号联合
 * @param arr
 * @returns {string}
 */
export function join(arr) {
  return arr ? arr.join(',') : '';
}

/**
 * 根据逗号分隔
 * @param str
 * @returns {string}
 */
export function split(str) {
  return str ? String(str).split(',') : '';
}

/**
 * 金额格式化
 * number：要格式化的数字
 * decimals：保留几位小数
 */
export function numberFormat(number, decimals) {
  let dec_point='.';
  let thousands_sep=',';
  number = (number + '').replace(/[^0-9+-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function (n, prec) {
      var k = Math.pow(10, prec);
      return '' + Math.round(n * k) / k;
    };

  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  var re = /(-?\d+)(\d{3})/;
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, "$1" + sep + "$2");
  }

  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}
/** 数字金额大写转换(可以处理整数,小数,负数) */
export function smalltoBIG(n)
{
  var fraction = ['角', '分'];
  var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟']  ];
  var head = n < 0? '欠': '';
  n = Math.abs(n);

  var s = '';

  for (var i = 0; i < fraction.length; i++)
  {
    s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
  }
  s = s || '整';
  n = Math.floor(n);

  for (var i = 0; i < unit[0].length && n > 0; i++)
  {
    var p = '';
    for (var j = 0; j < unit[1].length && n > 0; j++)
    {
      p = digit[n % 10] + unit[1][j] + p;
      n = Math.floor(n / 10);
    }
    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零')  + unit[0][i] + s;
  }
  return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}

/**
 * 浮点型相减(返回值是String类型)
 */
export function accSub(num1,num2){
  var r1,r2,m,n;
  try{
    r1 = num1.toString().split('.')[1].length;
  }catch(e){
    r1 = 0;
  }
  try{
    r2=num2.toString().split(".")[1].length;
  }catch(e){
    r2=0;
  }
  m=Math.pow(10,Math.max(r1,r2));
  n=(r1>=r2)?r1:r2;
  return (Math.round(num1*m-num2*m)/m).toFixed(n);
}

// 两个浮点数求和
export function accAdd(num1,num2){
  var r1,r2,m;
  try{
    r1 = num1.toString().split('.')[1].length;
  }catch(e){
    r1 = 0;
  }
  try{
    r2=num2.toString().split(".")[1].length;
  }catch(e){
    r2=0;
  }
  m=Math.pow(10,Math.max(r1,r2));
  // return (num1*m+num2*m)/m;
  return Math.round(num1*m+num2*m)/m;
}

// 浮点两数相除
export function accDiv(num1,num2){
  var t1,t2,r1,r2;
  try{
    t1 = num1.toString().split('.')[1].length;
  }catch(e){
    t1 = 0;
  }
  try{
    t2=num2.toString().split(".")[1].length;
  }catch(e){
    t2=0;
  }
  r1=Number(num1.toString().replace(".",""));
  r2=Number(num2.toString().replace(".",""));
  return (r1/r2)*Math.pow(10,t2-t1);
}

// 两数相乘
export function accMul(num1,num2){
  var m=0,s1=num1.toString(),s2=num2.toString();
  try{m+=s1.split(".")[1].length}catch(e){};
  try{m+=s2.split(".")[1].length}catch(e){};
  return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
